from django.db import models
from django.utils.translation import ugettext_lazy as _
from mptt.models import MPTTModel, TreeForeignKey
from django.contrib.sites.models import Site

class Page(MPTTModel):
    title = models.CharField(max_length=255, verbose_name=_('Title'), help_text=_('Page\'s title.'))
    slug = models.CharField(max_length=100, verbose_name=_('Slug'), help_text=_('Pages slug'))
    parent = TreeForeignKey('self', verbose_name=_('Parent'), null=True, blank=True, related_name='children')
    code = models.TextField(verbose_name=_('Page\'s Code'), help_text=_('Pages Code Template. That can be plain text, html or django template\'s code.'))
    template = models.ForeignKey('Template', verbose_name=_('Template'))
    seo_keywords = models.TextField(
        verbose_name=_('SEO Keywords'),
        help_text=_('type plain text and/or django templates variables {{ variable.name }}'),
        blank=True, null=True
    )
    seo_description = models.TextField(
        verbose_name=_('SEO Description'),
        help_text=_('type plain text and/or django templates variables {{ variable.name }}'),
        blank=True, null=True
    )
    seo_title = models.TextField(
        verbose_name=_('SEO Title'),
        help_text=_('type plain text and/or django templates variables {{ variable.name }}'),
        blank=True, null=True
    )
    site = models.ForeignKey(Site, verbose_name=_('Site'))
    redirect_to = models.TextField(max_length=65535, verbose_name=_('Redirect to'), null=True, blank=True)

    def __unicode__(self):
        return ('%s [%s]') % (self.title, self.pk)

    class Meta:
        ordering = ['title',]
        verbose_name = _('Page')
        verbose_name_plural = _('Pages')

    class MPTTMeta:
        order_insertion_by = ['title',]

class Style(models.Model):
    title = models.CharField(max_length=255, verbose_name=_('Title'))
    code = models.TextField(verbose_name=_('Code'), help_text=_('CSS stylesheet code'), blank=True, null=True)
    site = models.ForeignKey(Site, verbose_name=_('Site'))
    pages = models.ManyToManyField(Page, through='StyleRelationship')

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ['title',]
        verbose_name = _('Style')
        verbose_name_plural = _('Styles')

class Script(models.Model):
    title = models.CharField(max_length=255, verbose_name=_('Title'))
    code = models.TextField(verbose_name=_('Code'), help_text=_('Javascript code'), blank=True, null=True)
    is_file = models.BooleanField(default=False, verbose_name=_('Is File'))
    site = models.ForeignKey(Site, verbose_name=_('Site'))
    pages = models.ManyToManyField(Page, through='ScriptRelationship')

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ['title',]
        verbose_name = _('Script')
        verbose_name_plural = _('Scripts')

class StyleRelationship(models.Model):
    page = models.ForeignKey(Page, verbose_name=_('Page'))
    style = models.ForeignKey(Style, verbose_name=_('Style'))
    ordering = models.IntegerField(default=0, verbose_name=_('Ordering'))

    def __unicode__(self):
        return ('%s [%s] [%s]') % (self.page.title, self.style.title, self.ordering)

    class Meta:
        ordering = ['page__title', 'ordering',]
        verbose_name = _('Style Relationship')
        verbose_name_plural = _('Style Relationships')


class ScriptRelationship(models.Model):
    page = models.ForeignKey(Page, verbose_name=_('Page'))
    script = models.ForeignKey(Script, verbose_name=_('Script'))
    ordering = models.PositiveIntegerField(default=0, verbose_name=_('Ordering'))

    def __unicode__(self):
        return ('%s [%s] [%s]') % (self.page.title, self.script.title, self.ordering)

    class Meta:
        ordering = ['page__title', 'ordering',]
        verbose_name = _('Script Relationship')
        verbose_name_plural = _('Script Relationships')

class Snippet(models.Model):
    title = models.CharField(max_length=255, verbose_name=_('Title'))
    code = models.TextField(verbose_name=_('Code'), help_text=_('Code of the snippet. Plain text or html code or Django templates code'), null=True, blank=True)
    site = models.ForeignKey(Site, verbose_name=_('Site'))

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ['site', 'title',]
        verbose_name = _('Snippet')
        verbose_name_plural = _('Snippets')

class Template(MPTTModel):
    title = models.CharField(max_length=255, verbose_name=_('Title'))
    code = models.TextField(verbose_name=_('Code'), blank=True, null=True)
    parent = TreeForeignKey('self', verbose_name=_('Parent'), related_name='children', null=True, blank=True)
    site = models.ForeignKey(Site, verbose_name=_('Site'))

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ['title']
        verbose_name = _('Template')
        verbose_name_plural = _('Templates')

    class MPTTMeta:
        order_insertion_by = ['title',]
